<?php
require 'includePDO.php';
require 'config.php';
require '../smarty/Smarty.class.php';
require 'smartywt.php';



/*
 * 初始化模板函数
 */


#初始化数据库
try {
	//new PDO('mysql:host=' . $host .';dbname=' . $db, $user, $pass);
	$phpdb = new PDO('mysql:host=' . $host .';dbname=' . $db, $user, $passw);
	//$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
}
catch (PDOException $e)
{
	die ('Error!: ' . $e->getMessage() . '<br/>');
}

/*
 * 显示标题
*/

/*
 * 显示所有博文post
 * 显示博客信息blogInfo
 */
function showAll($num = '', $start = '')
{
	#导入模板
	require 'smartywt.php';
	$infoSQL = 'SELECT `BID` , `Blog_Title` , `Blog_Link` , `Blog_Feed` FROM blog_info';
	$blogInfo = wt_SQL::get_all($infoSQL);
	
	$title = '我的博客订阅';
	$smarty -> assign('title', $title);
	$smarty -> assign('blogInfo', $blogInfo);
	$smarty -> display('post.tpl');
}

/*
 * 更新博文
 * 默认更新全部
 */
function updata($rss = '')
{
	if(empty($rss))
	{
		$sql = 'SELECT `Blog_Feed` FROM blog_info';
		updataAll($sql);
	}
	else
	{
		$rssPost = wt_xml::readPost($rss);
		wt_post::addPost($rssPost);
	}
}

function updataAll($sql)
{
	$blogRss = wt_SQL::get_arr_num($sql);
	foreach ($blogRss as $rss)
	{
		$rssPost = wt_xml::readPost($rss);
		wt_post::addPost($rssPost);
	}
}
/*
 * 显示博客信息
 */
function blogInfo()
{
	$sql = 'SELECT `BID` , `Blog_Title` , `Blog_Link` , `Blog_Feed` FROM blog_info';
	$blogInfo = wt_SQL::get_all($sql);
}

/*
 * 搜索相关
 * $key：关键字
 * $post：搜索结果的返回值
 */
function search($key = '', $num = 10, $start = 0)
{	
	#导入模板
	require 'smartywt.php';
	$post = wt_search::so($key, $num = 10, $start = 0);
	//print_r($post);
	$title = '搜索_' . $key;
	page($post);
	$smarty -> assign('key', $key);
	$smarty -> assign('title', $title);
	$smarty -> assign('post', $post);
	$smarty -> display('main.tpl');
}



/*
 * 分页
 */
function page($post, $url='')
{
	echo count($post);
}
/**
 * 读取某一篇日志
 */
function readOne($url)
{
	#导入模板
	require 'smartywt.php';
	$sql = 'SELECT * FROM `post_into` WHERE `Post_Link` = \'' . $url . '\'';
	$post = wt_SQL::get_one($sql);
	//echo $sql;
	//print_r($post);
	$smarty -> assign('title', $post['Post_Title']);
	$smarty -> assign('Post_Link', $post['Post_Link']);
	$smarty -> assign('Post_PubDate', $post['Post_PubDate']);
	$smarty -> assign('Post_Content', $post['Post_Content']);
	$smarty -> assign('Post_Creator', $post['Post_Creator']);
	$smarty -> display('readpost.tpl');
}
/*
 * 读取源下所有日志
 */
function readRSS($rss, $num = 10, $start = 0)
{
	$sql = 'SELECT * FROM `post_into` WHERE `Post_Feed` = \'' . $rss . '\' ORDER BY Pid DESC';
	$sqlTitle = 'SELECT `Blog_Title` FROM `blog_info` WHERE `Blog_Feed` = \'' . $rss . ' \' LIMIT 1';
	$info = wt_SQL::get_one($sqlTitle);
	readPost($sql, $info['Blog_Title']);
}

/*
 * 返回作者下的所有文章
 */
function readCreator($creator)
{
	$sql = 'SELECT * FROM `post_into` WHERE `Post_Creator` = \'' . $creator . '\'';
	$title = '搜索_' . $creator;
	readPost($sql, $title);
}

/*
 * 查看文章
 */
function readPost($sql, $title, $num = 10, $start = 0)
{
	#导入模板
	require 'smartywt.php';
	$post = wt_SQL::selectLimit($sql, $num = 10, $start = 0);

	//print_r($post);
	$smarty -> assign('post', $post);
	$smarty -> assign('title', $title);
	$smarty -> display('main.tpl');
}
/*
 * 测试更新文章
 */
function d()
{
	$sql = 'SELECT * FROM `post_into`';
	$post = wt_SQL::selectLimit($sql);
	wt_post::getUnreadArticles($post);
}

wt_method::get($_GET);
